package com.hym.travel.controller;

import com.hym.travel.entity.User;
import com.hym.travel.service.serviceImpl.UserServiceImpl;
import com.hym.travel.utils.CreateImageCode;
import com.sun.org.apache.xerces.internal.impl.xs.models.XSDFACM;
import javafx.beans.binding.ObjectExpression;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin //允许跨域
@Slf4j
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/login")
    public Map<String,Object> login(@RequestBody User user){
        log.info("user:"+user);
        HashMap<String, Object> map = new HashMap<>();
        try {
            User userDB = userService.findUserByUsername(user);
            //登录成功后保存标记，用这种方法浪费缓存，应改为redis
            //request.getServletContext().setAttribute(userDB.getId(),userDB);
            map.put("data",userDB);
            map.put("msg","登录成功");
            map.put("status",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg","登录失败");
            map.put("status",false);
        }
        return map;
    }

    @RequestMapping("/register")
    public Map<String,Object> register(@RequestBody User user){
        Map<String, Object> map = new HashMap<>();
        try {
            userService.save(user);
            map.put("msg","注册成功");
            map.put("status",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status",false);
            map.put("msg","注册失败");
        }
        return map;
    }
    /**
     * @Auther: hyming
     * Description:  生成验证码
     * @Date :17:51 2020/11/8
     * @Return:
     * @Param:  * @param null
     */
    @RequestMapping("/getImage")
    public Map<String,Object> getImage(){
        Map<String, Object> map = new HashMap<>();
        CreateImageCode createImageCode = new CreateImageCode();
        //获取验证码
        String code = createImageCode.getCode();
        //生成图片
        BufferedImage buffImg = createImageCode.getBuffImg();
        String key = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());

        //进行64位编码
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            ImageIO.write(buffImg,"png",byteArrayOutputStream);
            String string = Base64Utils.encodeToString(byteArrayOutputStream.toByteArray());
            map.put(key,code);
            map.put("image",string);
            map.put("status",true);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("status",false);
        }
        return map;
    }
}
